define(function(require) {
	var $ = require("jquery");
	var justep = require("$UI/system/lib/justep");

	var Model = function() {
		this.callParent();
		this.isBack;	
	};
	
	//返回上一页
	Model.prototype.backBtnClick = function(event){
		justep.Shell.closePage();
		/*1、返回上一页 2、添加派发事件
		代码如下：
		justep.Shell.closePage();
		setTimeout(function(){
			justep.Shell.fireEvent("onRestoreContent", {});
		},500);
		*/
	};
	
	//图片路径转换
	Model.prototype.getImageUrl = function(url){
		return require.toUrl(url);
	};

	//全选
	Model.prototype.allChooseChange = function(event){
		var LoginState = localStorage.getItem('LoginState');
		var shoppingData = this.comp("shoppingData");
		var choose=this.comp("allChoose").val();
		shoppingData.each(function(param){
			var row = param.row;
			if(choose){
				if(row.val('fPhone') == LoginState && row.val('fComment') != '已购买')
					shoppingData.setValue("fChoose","1");
			} else {
				if(row.val('fPhone') == LoginState && row.val('fComment') != '已购买')
					shoppingData.setValue("fChoose","");
			}
		});
	};
	//单选
	Model.prototype.checkboxChange = function(event){
		var row = event.bindingContext.$object;
		var shoppingData = this.comp("shoppingData");
		var productData = this.comp("productData");
		var LoginState = localStorage.getItem('LoginState');
		shoppingData.each(function(param){
			var row = param.row;
			if(row.val('fPhone') == LoginState){
				row.val('fProID');
			}
			if(event.checked){
				
			}
		});
	};
	
	//减数量
	Model.prototype.reductionBtnClick = function(event){
		/*
		1、减少数量按钮绑定点击事件onClick()
		2、点击按钮，当前记录的fNumber值减1
		3、fNumber为1时不再相减
		*/
		var row = event.bindingContext.$object;
		var n=row.val("fNumber");
		if(n>1){
			row.val("fNumber",n-1);
		}
	};
	
	//加数量
	Model.prototype.addBtnClick = function(event){
		/*
		1、增加数量按钮绑定点击事件onClick()
		2、点击按钮，当前记录的fNumber值加1
		*/
		var row = event.bindingContext.$object;
		var n=row.val("fNumber");
		row.val("fNumber",n+1);
	};
	
	//删除
	Model.prototype.delBtnClick = function(event){
		var shoppingData = this.comp("shoppingData");
		var productData = this.comp("productData");
		var LoginState = localStorage.getItem('LoginState');
		var ID;
		productData.each(function(param){
			var row = param.row;
			   if(row.val('fChoose') == "1")
				   ID = row.val('fID');
			  });
		var shoppingRows = shoppingData.find(["fPhone","fProID"],[LoginState,ID]);
		shoppingData.deleteData(shoppingRows);
		shoppingData.saveData();
		
	};
	
	Model.prototype.showBackBtn = function(isBack){
		/*
		1、根据参数修改calculateData
		 		
		this.isBack = isBack;
		var v = isBack ? 1 : 0;
		this.comp("calculateData").setValue("isBack",v);
		*/
	};
	
	//结算事件
	Model.prototype.settlementClick = function(event){
		
		var LoginState = localStorage.getItem('LoginState');
		var shoppingData =this.comp('shoppingData');
		var productData =this.comp('productData');
		var ID = 0;
		shoppingData.each(function(param){
			var row = param.row;
			if(row.val('fChoose') == "1"){
				ID = 1;
				shoppingData.setValue("fChoose", 1);
				}
		});
		shoppingData.saveData();
		if (ID = 1)
			justep.Shell.showPage("buy",{});
		else{
			justep.Util.hint("请选择购买商品！");
			}
	};

	Model.prototype.listClick = function(event){
		/*
		 1、获取当前商品ID
		 2、传入弹出窗口，弹出窗口中显示商品详细信息
		 3、在弹出窗口的接收事件中，从服务端过滤数据
		*/
		
	};
	Model.prototype.Phone = function(row){
		var LoginState = localStorage.getItem('LoginState');
		return row.val('fPhone')== LoginState && row.val('fShop')=='待支付';
	};
	
	Model.prototype.modelModelConstructDone = function(event){
		var shoplist = this.comp("shoplist");
		shoplist.refresh();
	};
	
	Model.prototype.proClick = function(event){
		var data=this.comp("productData"); 
		var shoppingData =this.comp('shoppingData');
		var Id = shoppingData.getValue("fID");
	};

	
	
	
	
	return Model;
});